package com.killer.designcontrols;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.killer.designcontrols.utils.AnimUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView iv_home,iv_menu;
    private boolean isShowLevel2 = true;//是否显示二级菜单
    private boolean isShowLevel3 = true;
    private boolean isShowMenu = true;//是否显示整个菜单
    private String KILLER = "killer";
    private RelativeLayout leavel1,leavel2,leavel3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initListener();
    }



    private void initView() {
        setContentView(R.layout.activity_main);
        iv_home = (ImageView) findViewById(R.id.iv_home);
        iv_menu = (ImageView) findViewById(R.id.iv_menu);
        leavel1 = (RelativeLayout) findViewById(R.id.level1);
        leavel2 = (RelativeLayout) findViewById(R.id.level2);
        leavel3 = (RelativeLayout) findViewById(R.id.level3);

    }

    private void initListener() {
        iv_home.setOnClickListener(this);
        iv_menu.setOnClickListener(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_MENU){
            if (isShowMenu){
                int startOffset = 0;
                if (isShowLevel3){
                    AnimUtil.closeMenu(leavel3,startOffset);
                   isShowLevel3 = false;
                    startOffset += 200;
                }
                if (isShowLevel2){
                    AnimUtil.closeMenu(leavel2,startOffset);
                    isShowLevel2 = false;
                    startOffset += 200;
                }
                AnimUtil.closeMenu(leavel1,startOffset);

            }else {
                AnimUtil.showMenu(leavel1,0);
                AnimUtil.showMenu(leavel2,200);
                isShowLevel2 = true;
                AnimUtil.showMenu(leavel3,400);
                isShowLevel3 = true;
            }
            isShowMenu = !isShowMenu;
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_home:
                if (AnimUtil.animCount!=0){
                    //说明有动画在执行
                    return;
                }
                if (isShowLevel2){//需要隐藏

                    int startOffset = 0;
                    if (isShowLevel3){
                        AnimUtil.closeMenu(leavel3,startOffset);
                        startOffset += 200;//保证如果不走这里，则startOffset值不变
                        isShowLevel3 = false;
                    }
                    AnimUtil.closeMenu(leavel2,startOffset);
                }else {
                    AnimUtil.showMenu(leavel2,0);
                }
                isShowLevel2 = !isShowLevel2;
                break;
            case R.id.iv_menu:
                if (AnimUtil.animCount!=0){
                    //说明有动画在执行
                    return;
                }
                if (isShowLevel3){
                    //需要隐藏
                    AnimUtil.closeMenu(leavel3,0);
                }else {
                    AnimUtil.showMenu(leavel3,0);
                }
                isShowLevel3 = !isShowLevel3;
                break;
            default:
                break;
        }
    }
}
